﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Web.Code;
using Sausage.Common;
using Sausage.Web.Domain;
using Sausage.Web.Models;
using System.Web.Script.Serialization;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 养护计划控制器
    /// </summary>
    public class MaintainPlanController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Submit()
        {
            return View();
        }

        /// <summary>
        /// 审核。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Audit()
        {
            return View();
        }

        /// <summary>
        /// 确认。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Confirm()
        {
            return View();
        }

        /// <summary>
        /// 查看养护计划。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewPlan(Guid planID)
        {
            //返回视图
            return View(MaintainPlanHelper.GetMaintainPlanByID(planID));
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = MaintainPlanHelper.Select(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SubmitSelect(JosnPagingQueryModel query)
        {
            var result = MaintainPlanHelper.SubmitSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult AuditSelect(JosnPagingQueryModel query)
        {
            var result = MaintainPlanHelper.AuditSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ConfirmSelect(JosnPagingQueryModel query)
        {
            var result = MaintainPlanHelper.ConfirmSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid id)
        {
            var item = MaintainPlanHelper.GetMaintainPlanByID(id);
            if (item == null)
                return Json(new JsonResultViewModel { success = true, message = "找不到此养护计划！" },
                            JsonRequestBehavior.AllowGet);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    total = 1,
                    data = item,
                    message = "养护计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(MaintainPlanListRow data)
        {
            MaintainPlanHelper.DeleteMaintainPlan(data.PlanID);
            return Json(new JsonResultViewModel { success = true, message = "养护计划删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(MaintainPlanListRow data)
        {
            //一周一个单位，只能有一个养护计划，不管状态是否为提交。
            string errorMessage;
            if (!MaintainPlanHelper.CouldUpdateMaintainPlan(data.ApplicationDate, data.PlanID, out errorMessage))
                return Json(new JsonResultViewModel { success = false, message = errorMessage }, JsonRequestBehavior.AllowGet);

            MaintainPlanHelper.UpdateMaintainPlan(data);
            return Json(new JsonResultViewModel { success = true, message = "养护计划更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult UpdateState(Guid id, int state)
        {
            MaintainPlanHelper.UpdateMaintainPlanState(id, state);
            return Json(new JsonResultViewModel { success = true, message = "养护计划状态更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <returns>创建结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(MaintainPlanListRow data)
        {
            //一周一个单位，只能有一个养护计划，不管状态是否为提交。
            string errorMessage;
            if (!MaintainPlanHelper.CouldCreateMaintainPlan(data.ApplicationDate, out errorMessage))
                return Json(new JsonResultViewModel { success = false, message = errorMessage }, JsonRequestBehavior.AllowGet);

            if (!Helper.CouldSetPlan(data.ApplicationDate, out errorMessage))
                return Json(new JsonResultViewModel { success = false, message = errorMessage }, JsonRequestBehavior.AllowGet);

            MaintainPlanHelper.CreateMaintainPlan(data);
            return Json(new JsonResultViewModel { success = true, message = "养护计划创建成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="quantityID">The quantity ID.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSubmit(Guid[] plans)
        {
            MaintainPlanHelper.ExecuteSubmit(plans);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护计划提交成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="plans">The plans.</param>
        /// <param name="name">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteAudit(Guid[] plans, bool passed, string comment)
        {
            MaintainPlanHelper.ExecuteAudit(plans, passed, comment);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护计划审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 批准
        /// </summary>
        /// <param name="plans">The plans.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteConfirm(Guid[] plans, bool passed, string comment)
        {
            MaintainPlanHelper.ExecuteConfirm(plans, passed, comment);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护计划审核成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
